package com.czd.support.controller;

import com.czd.support.common.Constants;
import com.czd.support.model.DataDictionary;
import com.czd.support.model.Manager;
import com.czd.support.model.Question2;
import com.czd.support.service.AnswerQuestionService;
import com.czd.support.service.AnswerService;
import com.czd.support.service.DataDictionaryService;
import com.czd.support.service.QuestionService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Author: Jerry.hu
 * Create: Jerry.hu (12:22)
 * Description:
 * To change this template use File | Settings | File Templates.
 */
@Controller
@RequestMapping("/question")
public class QuestionController extends BaseController {

    @Autowired
    private QuestionService questionService;
    @Autowired
    private DataDictionaryService dataDictionaryService;
    @Autowired
    private AnswerService answerService;
    @Autowired
    private AnswerQuestionService answerQuestionService;

    /**
     * 题目 类型字典
     * @return
     */
    @ModelAttribute("questionTypeList")
    protected List<DataDictionary> initQuestionType(){
        return dataDictionaryService.queryListByCode(Constants.QUESTION_CODE);
    }

    /**
     * 题目列表页
     * @param model
     * @param question
     * @return
     */
    @RequestMapping(value = {"","/","list"},method = RequestMethod.GET)
    public String list(Model model,Question2 question,@RequestParam(value = "pageNo",defaultValue = "1") int pageNo){
        question.setPageNo(pageNo);
        model.addAttribute("paginate",questionService.queryForPaginate(question));
        return "question/list";
    }

    /**
     * 新增题目控制器
     * @return
     */
    @RequestMapping(value = "/add",method = RequestMethod.GET)
    public String add(){
        return "question/add";
    }

    /**
     * 录入题目 拦截
     * @param question
     * @param request
     * @return
     */
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public String save(Question2 question,HttpServletRequest request,@RequestParam(value = "aId[]") int [] aId,Model model){
        Manager manager = this.getSesionUser(request);
        question.setCreater(manager.getUserCode());
        question.setUpdater(manager.getUserCode());
        if(StringUtils.isBlank(question.getQuestionContent())){
            model.addAttribute("error","题目不许为空");
        }else {
            if (questionService.insert(question)) {
                if (answerQuestionService.replaceInto(question.getId(), aId)) {
                    return "redirect:/question/"+question.getId()+"/details";
                } else {
                    return error(model, "操作失败", "更新题目信息失败");
                }
            }
        }
        model.addAttribute("error","题目录入失败");
        return "question/add";
    }

    /**
     * 题目编辑 显示拦截
     * @param id
     * @param model
     * @return
     */
    @RequestMapping(value = "/{id}/edit",method = RequestMethod.GET)
    public String change(@PathVariable int id,Model model){
        Question2 question = questionService.findById(id);
        if(question != null){
            model.addAttribute("question",question);
            model.addAttribute("answerList", answerService.queryAnswersByQId(question.getId()));
            return "question/edit";
        }
        return error(model,"信息不存在","题目不存在");
    }

    /**
     * 题目编辑入库 拦截
     * @param id
     * @param question
     * @param request
     * @return
     */
    @RequestMapping(value = "/{id}/edit",method = RequestMethod.POST)
    public String update(@PathVariable int id,Question2 question,HttpServletRequest request,@RequestParam(value = "aId[]") int [] aId,Model model){
        Question2 question2 = questionService.findById(id);
        if(question2 != null){
            Manager manager = this.getSesionUser(request);
            question.setUpdater(manager.getUserCode());
            if(StringUtils.isBlank(question.getQuestionContent())){
                model.addAttribute("error","题目不许为空");
            }else {
                if (questionService.update(question)) {
                    if (answerQuestionService.replaceInto(question.getId(), aId)) {
                        return "redirect:/question/{id}/details";
                    } else {
                        return error(model, "操作失败", "更新题目信息失败");
                    }
                }
            }
            model.addAttribute("error","题目更新失败");
            model.addAttribute("question",question);
            model.addAttribute("answerList", answerService.queryAnswersByQId(question.getId()));
            return "question/edit";
        }else{
            return error(model, "操作失败", "题目已经不存在");
        }
    }

    /**
     * 题目和答案 关联拦截
     * @param id
     * @param aId
     * @param model
     * @return
    @RequestMapping(value = "/{id}/{aId}",method = RequestMethod.POST)
    public String saveQA(@PathVariable int id,@PathVariable String aId,Model model){
        if(id> 0){
            if(answerQuestionService.replaceInto(id,aId)){
                return "redirect:/question/{id}/details";
            }else{
               return error(model,"操作失败","更新题目信息失败");
            }
        }
        return error(model,"题目更新","题目主要信息错误");
    }*/

    /**
     * 题目详情页 拦截
     * @param id
     * @param model
     * @return
     */
    @RequestMapping(value = "/{id}/details",method = RequestMethod.GET)
    public String view(@PathVariable int id,Model model){
        Question2 question = questionService.findById(id);
        if(question != null){
            model.addAttribute("question",question);
            model.addAttribute("answerList", answerService.queryAnswersByQId(question.getId()));
            return "question/details";
        }
        return error(model,"信息不存在","题目不存在");
    }
}

